package com.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class MainApp {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                new WeatherAppGUI("f189272074e94c8b35261b76e4b526fa");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "应用初始化失败: " + e.getMessage());
            }
        });
    }
}

class WeatherAppGUI extends JFrame {
    private final WeatherApiClient apiClient;
    private final WeatherDataParser dataParser;
    private WeatherDisplay currentDisplay;
    private JTextField locationField;

    public WeatherAppGUI(String apiKey) {
        super("天气预报应用");
        this.apiClient = new OpenWeatherMapClient(apiKey);
        this.dataParser = new OpenWeatherMapParser();

        initializeUI();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 400);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void initializeUI() {
        setLayout(new BorderLayout());

        // 创建顶部面板
        JPanel topPanel = new JPanel(new FlowLayout());
        locationField = new JTextField(20);
        JButton searchButton = new JButton("查询天气");
        
        JComboBox<String> displayStyleCombo = new JComboBox<>(new String[]{"简洁视图", });//{"简洁视图", "详细视图"});
        displayStyleCombo.addActionListener(e -> switchDisplayStyle(displayStyleCombo.getSelectedIndex()));

        searchButton.addActionListener(e -> fetchWeatherData());

        topPanel.add(new JLabel("城市:"));
        topPanel.add(locationField);
        topPanel.add(searchButton);
        //topPanel.add(new JLabel("显示样式:"));
        //topPanel.add(displayStyleCombo);

        add(topPanel, BorderLayout.NORTH);

        // 默认显示样式
        currentDisplay = new SimpleTextDisplay();
        add(currentDisplay, BorderLayout.CENTER);

        // 底部状态栏
        add(new JLabel("使用OpenWeatherMap API", JLabel.CENTER), BorderLayout.SOUTH);
    }

    private void switchDisplayStyle(int styleIndex) {
        getContentPane().remove(currentDisplay);
        currentDisplay = styleIndex == 0 ? new SimpleTextDisplay() : new DetailedGraphicalDisplay();
        add(currentDisplay, BorderLayout.CENTER);
        revalidate();
        repaint();
    }

    private void fetchWeatherData() {
        String location = locationField.getText().trim();
        if (location.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入城市名称");
            return;
        }

        try {
            String jsonData = apiClient.fetchWeatherData(location);
            WeatherData data = dataParser.parse(jsonData);
            currentDisplay.updateWeather(data);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "获取天气数据失败: " + ex.getMessage());
        }
    }
}